package email;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class CustomMessageTableModel extends AbstractTableModel{

	    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// These are the names for the table's columns.
    private static final String[] columnNames = {"Sender",
    "Subject", "Date"};
    
    // The table's list of messages.
    private ArrayList<ReceivedMessage> messageList = new ArrayList<ReceivedMessage>();

    // Sets the table's list of messages.
    public void setMessages(ArrayList<ReceivedMessage> messages) {
        messageList.clear();
    	for (int i = messages.size() - 1; i >= 0; i--) {
            messageList.add(messages.get(i));
        }
        // Fire table data change notification to table.
        fireTableDataChanged();
    }
    
    public void setMessage(int index, ReceivedMessage message){
    	if(index >= 0) messageList.add(index, message);
    	else messageList.add(message);
    }
    
    // Get a message for the specified row.
    public ReceivedMessage getMessage(int row) {
        return messageList.get(row);
    }
    
    
    public void createThreadMessage(int row, String name){
    	ReceivedMessage emulate = messageList.get(row);
    	//ReceivedMessage dummymessage = new ReceivedMessage(emulate);
		emulate.setSubject(name);
    	messageList.remove(row);
    	messageList.add(row, emulate);
    	fireTableRowsUpdated(row, row);
    }
    
    public void createThreadMessage(ReceivedMessage message, String name){
    	int row = getRowForMessage(message);
    	createThreadMessage(row, name);
    }
    
    public int getRowForMessage(ReceivedMessage message){
    	long ourmessageid = Util.createId(message);
    	int row = 0;
    	for(int i = 0; i < messageList.size(); i++){
    		ReceivedMessage m = messageList.get(i);
    		long themessageid = Util.createId(m);
			if(themessageid == ourmessageid){
				row = i;
				break;
			}
    	}
    	return row;
    }
    
    // Remove a message from the list.
    public void deleteMessage(int row) {
        messageList.remove(row);
        
        // Fire table row deletion notification to table.
        fireTableRowsDeleted(row, row);
    }
    
    public void deleteMessage(ReceivedMessage message){
    	int row = getRowForMessage(message);
    	deleteMessage(row);
    }
    
    
    // Get table's column count.
    public int getColumnCount() {
        return columnNames.length;
    }
    
    // Get a column's name.
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    // Get table's row count.
    public int getRowCount() {
        return messageList.size();
    }
    
    // Get value for a specific row and column combination.
    public Object getValueAt(int row, int col) {
        try {
            ReceivedMessage message = messageList.get(row);
            switch (col) {
                case 0: // Sender
                    String sender = message.getFrom();
                    if (sender != null) {
                        return sender;
                    } else {
                        return "[none]";
                    }
                case 1: // Subject
                    String subject = message.getSubject();
                    if (subject != null && subject.length() > 0) {
                        return subject;
                    } else {
                        return "[none]";
                    }
                case 2: // Date
                    String date = message.getDate();
                    if (date != null) {
                        return date.toString();
                    } else {
                        return "[none]";
                    }
            }
        } catch (Exception e) {
            // Fail silently.
            return "";
        }
        
        return "";
    }
    
    public void clearTable(){
    	messageList.clear();
    }
    
    public ArrayList<ReceivedMessage> getAllMessages(){
    	return messageList;
    }
}

